package com.yunhe.tianhe.rentcars.controller.permission;

import com.yunhe.tianhe.commons.dto.Pageable;
import com.yunhe.tianhe.rentcars.dto.JSONResponse;
import com.yunhe.tianhe.rentcars.dto.TreeNode;
import com.yunhe.tianhe.rentcars.entity.permission.Role;
import com.yunhe.tianhe.rentcars.service.permission.ResourceService;
import com.yunhe.tianhe.rentcars.service.permission.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lanyangyun
 * @version V1.0
 * @Description: //TODO
 * @date 2017/10/19 0019 上午 11:47
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService rs;
    @Autowired
    private ResourceService ps;

    @RequestMapping
    public String list(HttpServletRequest req){
        return "/permission/role/list";
    }

    @RequestMapping("/add")
    public String add(){
        return "/permission/role/add";
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest req, Long roleId){
        Role role = rs.find(roleId);
        req.setAttribute("role",role);
        return "/permission/role/edit";
    }

    @RequestMapping("/save")
    public String save(Role role,String ids){
        rs.save(role,ids);
        return "redirect:/role";
    }

    @RequestMapping("/update")
    public String update( Role role,String ids){
        rs.update(role,ids);
        return "redirect:/role";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResponse delete(String  ids){
        rs.deleteBatch(ids);
        return JSONResponse.success("删除成功！");
    }

    @RequestMapping("/pageData")
    @ResponseBody
    public Pageable<Role> findPage(Integer offset, Integer limit){
        Pageable<Role> pageable = rs.findPage(offset, limit);
        return pageable;
    }

    @RequestMapping("/tree")
    @ResponseBody
    public JSONResponse tree(Long roleId){
        List<TreeNode> treeNodes = ps.findTreeNodes(roleId);
        return JSONResponse.success("", treeNodes);
    }


    @RequestMapping("/gong")
    public String gong(){
        return "/permission/gong";
    }

    @RequestMapping("/moshi")
    public String moshi(){
        return "/permission/moshi";
    }

    @RequestMapping("/sheng")
    public String sheng(){
        return "/permission/sheng";
    }

    @RequestMapping("/cang")
    public String cang(){
        return "/permission/cang";
    }

}
